﻿using System;
using System.Collections.Generic;
using DynamicChart.Service.Step;

namespace DynamicChart.Service.FunctionConversion;

public static class ConvertStrategyFactory
{
    private static readonly Dictionary<string, Func<ISqlFunctionAdapter, IConvertStrategy>> StrategyBuilders = new()
    {
        { "DATE", adapter => new DateConvertStrategy(adapter) },
        { "LIKE", adapter => new LikeConvertStrategy(adapter) },
        { "COUNT", adapter => new CountConvertStrategy(adapter) },
        { "EMPTY", adapter => new EmptyConvertStrategy(adapter) },
    };

    public static IConvertStrategy GetStrategy(string input, ISqlFunctionAdapter adapter)
    {
        if (string.IsNullOrEmpty(input)) return null;

        string methodName = input.Split('(')[0].Trim().ToUpper();

        if (StrategyBuilders.TryGetValue(methodName, out var builder))
        {
            return builder(adapter);
        }

        return new DefaultConvertStrategy(input, adapter);
    }
}